﻿var clickedSession;
$(function () {
    ajaxCall('../DataService.asmx/GetAllEvents', true, "",
                function (msg) {
                    if (msg.d == null) {
                        $('#ddlEvents').append($('<option></option>').html("No events in the database"));
                    } else {
                        for (i in msg.d) {
                            $('#ddlEvents').append($('<option></option>').val(msg.d[i].EventName).html(msg.d[i].EventName));
                        }
                    }
                },
                function () {
                    alert("Error happend while reading from database");
                }
            );

    getAllSessions();

    $('#ddlEvents').change(function () {
        window.location = "ManageSessions.aspx#" + $(this).val();
    });


    $('.btnSessionDescription').click(function () {
        if ($(this).parent('.sessionDetails').children('.sessionDescription').is(':hidden')) {
            $(this).parent('.sessionDetails').children('.sessionDescription').show("slide", { direction: "down" }, 1000);
            $(this).parent('.sessionDetails').children('.sessionDescription').css('visibility', 'visible');
            $(this).html('Hide description');
        }
        else {
            $(this).parent('.sessionDetails').children('.sessionDescription').hide("slide", { direction: "down" }, 1000);
            $(this).parent('.sessionDetails').children('.sessionDescription').css('visibility', 'hidden');
            $(this).html('Show description');
        }
    });


});

function getAllSessions() {
    ajaxCall('../DataService.asmx/GetAllSessions', false, "",
                function (msg) {
                    if (msg.d == null) {
                        $('#SessionTemp').html("There are no sessions in the database");
                    } else {
                        for (var i in msg.d) {
                            var speakers = msg.d[i].Speakers[0].FirstName + " " + msg.d[i].Speakers[0].LastName;
                            for (var j = 1; j < msg.d[i].Speakers.length; j++) {
                                speakers = speakers + " & " + msg.d[i].Speakers[j].FirstName + " " + msg.d[i].Speakers[j].LastName;
                            }
                            var sessions = [
                                { ID: msg.d[i].ID, Title: msg.d[i].Title, Description: msg.d[i].Description, Language: msg.d[i].Language, Speakers: speakers, EventName: msg.d[i].EventName }
                            ];
                            $('#SessionTemplate').tmpl(sessions).appendTo('#SessionTemp');
                        }
                    }
                },
                function () {
                    alert("Error hapend while reading from database");
                }

            );
}

function DeleteSession(id) {
    if (confirm("Are you sure that you want to delete this item?")) {
        var data = '{"id": "' + id + '"}';
        ajaxCall('../DataService.asmx/DeleteSession', false, data,
                    function () {
                        alert("Item deleted successfully");
                        $('#SessionTemp').empty();
                        getAllSessions();
                    },
                    function () {
                        alert("Error happend while deleting item");
                    }
                );

    }

}

function InsertImagesUrl(id) {
    $('#smallDialog').dialog({
        modal: true,
        closeOnEscape: false
    });
    clickedSession = id;
}

function InsertUrl() {
    if ($('#txtImageUrl').val() == "") {
        $('#lblSkyDriveLink').css('display', 'inherit');
    } else {
        debugger;
        $('#lblSkyDriveLink').css('display', 'none');
        ajaxCall('../DataService.asmx/InsertImagesForSession', false, "{'id':'" + clickedSession + "', 'url':'" + $('#txtImageUrl').val() +"'}",
            function () {
                alert("Insert was successful");
                $('#smallDialog').dialog("close");
            },
            function () {
                alert("Error happend while updating data to database");
            }
        );
    }
    
}